package combiner;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class DataCombine {

	public static String trainPath1 = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/result_set/train_result.txt";
	public static String validationPath1 = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/result_set/validation_result.txt";
	public static String testPath1 = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/result_set/test_result.txt";
	
	public static String trainPath2 = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/train_out_linear.txt";
	public static String validationPath2 = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/valid_out_linear.txt";
	public static String testPath2 = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/test_out_linear.txt";
	
	public static String trainCombinePath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/combine/train_result.txt";
	public static String validationCombinePath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/combine/validation_result.txt";
	public static String testCombinePath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/combine/test_result.txt";
	
	public void combineTrain() throws Exception{
		
		BufferedReader br1 = new BufferedReader(new FileReader(DataCombine.trainPath1));
		BufferedReader br2 = new BufferedReader(new FileReader(DataCombine.trainPath2));
		BufferedWriter bw = new BufferedWriter(new FileWriter(DataCombine.trainCombinePath));
		
		String line1 = br1.readLine();
		String line2 = br2.readLine();
		
		float svd;
		float lnr;
		float ans;
		
		int l = 0;
		while (line1 != null && line2 != null){
			if (l % 1000000 == 0) System.out.println(l); 
			l++;
			
			String[] s1 = line1.split(",");
			String[] s2 = line2.split(",");
			
			svd = Float.parseFloat(s1[2]);
			lnr = Float.parseFloat(s2[2]);
			ans = Float.parseFloat(s2[3]);
			
			bw.write("" + svd + "," + lnr + "," + ans + "\n");
			
			line1 = br1.readLine();
			line2 = br2.readLine();
		}
		
		br1.close();
		br2.close();
		bw.close();
		
		System.out.println(l);
		
	}
	
	public void combineTest() throws Exception{
		BufferedReader br1 = new BufferedReader(new FileReader(DataCombine.testPath1));
		BufferedReader br2 = new BufferedReader(new FileReader(DataCombine.testPath2));
		BufferedWriter bw = new BufferedWriter(new FileWriter(DataCombine.testCombinePath));
		
		String line1 = br1.readLine();
		String line2 = br2.readLine();
		
		float svd;
		float lnr;
		float ans;
		int l = 0;
		
		while (line1 != null && line2 != null){
			l++;
			String[] s1 = line1.split(",");
			String[] s2 = line2.split(",");
			
			svd = Float.parseFloat(s1[2]);
			lnr = Float.parseFloat(s2[2]);
			ans = Float.parseFloat(s2[3]);
			
			bw.write("" + svd + "," + lnr + "," + ans + "\n");
			
			line1 = br1.readLine();
			line2 = br2.readLine();
		}
		
		br1.close();
		br2.close();
		bw.close();
		
		System.out.println(l);
		
	}
	
	public void combineValidation() throws Exception{
		BufferedReader br1 = new BufferedReader(new FileReader(DataCombine.validationPath1));
		BufferedReader br2 = new BufferedReader(new FileReader(DataCombine.validationPath2));
		BufferedWriter bw = new BufferedWriter(new FileWriter(DataCombine.validationCombinePath));
		
		String line1 = br1.readLine();
		String line2 = br2.readLine();
		
		float svd;
		float lnr;
		float ans;
		int l = 0;
		
		while (line1 != null && line2 != null){
			
			l++;
			String[] s1 = line1.split(",");
			String[] s2 = line2.split(",");
			
			svd = Float.parseFloat(s1[2]);
			lnr = Float.parseFloat(s2[2]);
			ans = Float.parseFloat(s2[3]);
			
			bw.write("" + svd + "," + lnr + "," + ans + "\n");
			
			line1 = br1.readLine();
			line2 = br2.readLine();
		}
		
		br1.close();
		br2.close();
		bw.close();
		
		System.out.println(l);
	}
	
	public static void main(String[] args){
		DataCombine combiner = new DataCombine();
		try{
			combiner.combineTest();
			combiner.combineValidation();
			combiner.combineTrain();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}
